<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="dataForm.author" placeholder="作者"></el-input>
      </el-form-item>
      <el-form-item label="发布日期" prop="time">
        <el-input v-model="dataForm.time" placeholder="发布日期"></el-input>
      </el-form-item>
      <el-form-item label="点击量" prop="clicks">
        <el-input v-model="dataForm.clicks" placeholder="点击量"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <!-- <el-upload
          class="upload-demo"
          drag
          action=""
          :on-success="handle_success"
          multiple
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload> -->
        <el-upload
          class="upload-demo"
          action="http://124.223.70.80:8011/pdf"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="uploadSuccess"
          :before-remove="beforeRemove"
          multiple
          :limit="1"
          accept=".pdf" 
          :on-exceed="handleExceed"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传pdf文件，且不超过500kb
          </div>
        </el-upload>
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        title: "",
        author: "",
        time: "",
        clicks: "",
        content: "",
      },
      dataRule: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        author: [{ required: true, message: "作者不能为空", trigger: "blur" }],
        time: [
          { required: true, message: "发布日期不能为空", trigger: "blur" },
        ],
        clicks: [
          { required: true, message: "点击量不能为空", trigger: "blur" },
        ],
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/informate/zcjs/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.title = data.zcjs.title;
              this.dataForm.author = data.zcjs.author;
              this.dataForm.time = data.zcjs.time;
              this.dataForm.clicks = data.zcjs.clicks;
              this.dataForm.content = data.zcjs.content;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/informate/zcjs/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              title: this.dataForm.title,
              author: this.dataForm.author,
              time: this.dataForm.time,
              clicks: this.dataForm.clicks,
              content: this.dataForm.content,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },uploadSuccess(response,file){
      console.log(file)
     this.dataForm.content=response.data[0].alt

    }
  },
};
</script>
